﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.MembershipService;
using Avatar.Model;
using System.Web.Security;

public partial class Admin_UserGroup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btn_AddPanel.Visible = true;
            PanelAdd.Visible = false;
            PanelAddRemoveUser.Visible = false;
            PanelEdit.Visible = false;


        }
    }
    protected void btn_DUpdate_Click(object sender, EventArgs e)
    {
        Button _b = sender as Button;
        int _id = Convert.ToInt32(_b.CommandArgument);
        var _d = UserGroupManager.GetUserGroup(_id);
        if (_d != null)
        {
            PanelEdit.Visible = true;
            PanelAdd.Visible = false;
            btn_AddPanel.Visible = false;
            PanelAddRemoveUser.Visible = false;

            txt_NameUpdate.Text = _d.Name;
            txt_HomePageUpdate.Text = _d.HomePage;
            drp_PublicUpdate.SelectedValue = _d.Public == true ? "true" : "false";
            btn_DUpdateSubmit.CommandArgument = _d.ID.ToString();
        }
    }
    protected void btn_DAddSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (UserGroupManager.CreateGroup(txt_NameAdd.Text, txt_HomePageAdd.Text, drp_PublicUpdate.SelectedValue.ToLower() == "true" ? true : false))
            {
                txt_NameAdd.Text = "";
                txt_HomePageAdd.Text = "";
                
                GridView1.DataBind();

                btn_AddPanel.Visible = true;
                PanelAdd.Visible = false;
                
            }
            else
            {
                throw new Exception("添加管理单元失败：");
            }
        }
        catch (Exception _e)
        {
            lbl_AddError.Text = "*" + _e.Message;
        }
    }
    protected void btn_DDelete_Click(object sender, EventArgs e)
    {
        PanelEdit.Visible = false;
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);

            UserGroupManager.Delete(_id);

            GridView1.DataBind();
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "操作失败:" + _e.Message;
        }
    }
    protected void btn_DUpdateSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);
            if (!UserGroupManager.Update(_id, txt_NameUpdate.Text, txt_HomePageUpdate.Text, drp_PublicUpdate.SelectedValue == "true" ? true : false))
            {
                throw new Exception("更新失败,请重试.");
            }
            else
            {
                PanelEdit.Visible = false;
                PanelAdd.Visible = false;
                btn_AddPanel.Visible = true;
            }
            GridView1.DataBind();
        }
        catch (Exception _e)
        {
            lbl_EditError.Text = "*" + _e.Message;
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        PanelEdit.Visible = false;
        PanelAdd.Visible = false;
        btn_AddPanel.Visible = true;
        PanelAddRemoveUser.Visible = false;

        GridView1.DataBind();
    }
    protected void btn_AddRemoveUser_Click(object sender, EventArgs e)
    {
        PanelAddRemoveUser.Visible = true;
        btn_AddPanel.Visible = false;
        PanelEdit.Visible = false;
        PanelAdd.Visible = false;
        try
        {
            Button _b = sender as Button;
            int _id = Convert.ToInt32(_b.CommandArgument);
            var _group = UserGroupManager.GetUserGroup(_id);
            if (_group != null)
            {
                lbl_GroupName.Text = _group.Name;
                btn_AddRemoveUser.CommandArgument = _id.ToString();
            }
        }
        catch (Exception)
        {
            GridView1.DataBind();
        }
    }
    protected void btn_AddPanel_Click(object sender, EventArgs e)
    {
        PanelAdd.Visible = true;
        btn_AddPanel.Visible = false;
    }
    protected void btn_SearchUser_Click(object sender, EventArgs e)
    {
        try
        {
            List<Avatar_User> _userList = new List<Avatar_User>();
            if (drp_KeyWordKind.SelectedIndex == 0)
            {
                _userList = UserManager.SearchUserByKeyWord(txt_KeyWord.Text);
            }
            else
            {
                _userList = UserManager.SearchUserByEmail(txt_KeyWord.Text);
            }
            if (_userList.Count > 0)
            {
                drp_UserList.DataSource = _userList;
                drp_UserList.DataTextField = "Name";
                drp_UserList.DataValueField = "ID";
                drp_UserList.DataBind();
            }
            else
            {
                throw new Exception("没有搜索到任何符合查询条件的记录信息");
            }
        }
        catch (Exception _e)
        {
            lbl_AddRemoveError.Text ="出错啦:"+ _e.Message;
        }
    }
    protected void btn_GroupAddUser_Click(object sender, EventArgs e)
    {
        try
        {
            Button _b = sender as Button;
            int _uid = string.IsNullOrEmpty(drp_UserList.SelectedValue) ? 0 : Convert.ToInt32(drp_UserList.SelectedValue);
            int _gid = Convert.ToInt32(_b.CommandArgument);
            if (UserGroupManager.JoinGroup(_uid, _gid))
            {
                txt_KeyWord.Text = "";
                drp_UserList.Items.Clear();
                drp_KeyWordKind.SelectedIndex = 0;

                PanelAddRemoveUser.Visible = false;
                PanelAdd.Visible = false;
                PanelEdit.Visible = false;
                btn_AddPanel.Visible = true;

                GridView1.DataBind();
            }
            else
            {
 
            }
        }
        catch (Exception _e)
        {
            lbl_AddRemoveError.Text = _e.Message;
        }
    }
}